################################################################################-
# Replication File for Wratil, Wäckerle and Proksch: Government Rhetoric and the 
# Representation of Public Opinion in International Negotiations
#
# This script runs the participation (Appendix D) and the voting analysis (Appendix E),
# which is also presented in Figure 5 and the discussion in the main paper.
#
# Additionally, it produces the following graph:
# Table D1
# Table D2
# Table E1
# Table E2
# Figure E1
# Figure 5
################################################################################-

##############-
# Packages ####
##############-

source("99_functions.R")
library(tidyverse)   #version 1.3.2
library(lme4)        #version 1.1-30
library(broom.mixed) #version 0.2.9.4
library(ggeffects)   #version 1.1.2
library(effects)     #version 4.2-2
library(stargazer)   #version 5.2.3

##################################-
#### Participation Model ####
##################################-

load("generated_data/participation_dataset.RData")
prop.table(table(possible_speeches$spoke))
names(possible_speeches)

summary(possible_speeches$inflation)

mod_speech_participation_1 <- glmer(spoke ~ gov_eu_supporter*image_lag6m_scaled+final_two_months+unanimity_any+budget_any+
                                      gov_lr_cmp_static_scaled+eu_receipts_gdp_scaled+accession_year_scaled+days_in_office_scaled+
                                      unemployment_scaled+inflation_scaled+north_south+large_small+Council_Config_final+
                                      (1|Actor)+(1|Transcription), data = possible_speeches, 
                                    family = binomial(link = "logit"),
                                    control = glmerControl(optimizer = "bobyqa"),
                                    nAGQ = 1)
mod_speech_participation_2 <- glmer(spoke ~ gov_eu_supporter*image_lag6m_scaled+distance_elect_planned_scaled+unanimity_any+budget_any+
                                      gov_lr_cmp_static_scaled+eu_receipts_gdp_scaled+accession_year_scaled+days_in_office_scaled+
                                      unemployment_scaled+inflation_scaled+north_south+large_small+Council_Config_final+
                                      (1|Actor)+(1|Transcription), data = possible_speeches, 
                                    family = binomial(link = "logit"),
                                    control = glmerControl(optimizer = "bobyqa"),
                                    nAGQ = 1)

stargazer(mod_speech_participation_1,mod_speech_participation_2,
                     single.row = TRUE,align=TRUE,type="text")

# This is Table D1
stargazer(mod_speech_participation_1,mod_speech_participation_2,
                     single.row = TRUE,align=TRUE,
                     dep.var.labels   = "Debate Participation",
                     covariate.labels = c("Intercept","Eurosceptic government", "Public image of the EU", 
                                          "Last two months before election","Distance to planned election",
                                          "Unanimity required","Budget debate",
                                          "Government left-right position", 
                                          "Net receipts from EU budget",
                                          "Year of Accession","Time in Office","Unemployment rate","Inflation rate",
                                          "Northern country","Southern country","Small member state","Council Configuration: Ecofin",
                                          "Council Configuration: EPSCO","Council Configuration: ENV","Council Configuration: JHA",
                                          "Eurosceptic government x Public image of the EU"),
                     omit.stat = c("f","ll","ser"),
                     intercept.bottom = FALSE)

ggeffect(mod_speech_participation_1, type = "pred", 
                       terms = c("final_two_months"))

m.plot.gov <- ggeffect(mod_speech_participation_1, type = "pred", 
                                  terms = c("image_lag6m_scaled[all]","gov_eu_supporter"))
data.ranges <- possible_speeches %>% 
  group_by(gov_eu_supporter) %>% 
  summarise(min_values = min(image_lag6m_scaled),
            max_values = max(image_lag6m_scaled))

m.plot.gov <- m.plot.gov %>% 
  filter(group=="Europhile Government" & between(x,
                                                 data.ranges %>% filter(gov_eu_supporter=="Europhile Government") %>% select(min_values) %>% unlist(),
                                                 data.ranges %>% filter(gov_eu_supporter=="Europhile Government") %>% select(max_values) %>% unlist())|
           group=="Eurosceptic Government" & between(x,
                                                     data.ranges %>% filter(gov_eu_supporter=="Eurosceptic Government") %>% select(min_values) %>% unlist(),
                                                     data.ranges %>% filter(gov_eu_supporter=="Eurosceptic Government") %>% select(max_values) %>% unlist()))

m.plot.gov$group <- m.plot.gov$group %>% 
  recode("Europhile Government" = "Pro-EU \nGovernment",
         "Eurosceptic Government" = "Eurosceptic \nGovernment")

m.plot.gov %>% 
  group_by(group) %>% 
  summarise(mean_spoken = mean(predicted))

p_participation <- ggplot(m.plot.gov,aes(x=x,y=predicted))+
  geom_smooth(se=F,size=0.75,colour="black")+
  geom_smooth(aes(y=conf.low),size=0.75,se=F,lty="dashed",colour="black")+
  geom_smooth(aes(y=conf.high),size=0.75,se=F,lty="dashed",colour="black")+
  facet_wrap(~group)+
  labs(y="Predicted Probability of \nSpeaking in a Council Debate",x="Public Image of the EU (z-score)",
       title="a) Debate Participation")+
  theme_bw()+
  theme(strip.background = element_rect(fill = "white", colour = NA),
        axis.text = element_text(colour = "black"),
        axis.title = element_text(size=8,colour="black"),
        strip.text = element_text(colour = "black",face = "bold"))

##############-
# Voting ####
##############-

# read in voting dataset  
load(file="generated_data/all_votes.RData")

# Table of Voting Decisions
all_votes %>% 
  group_by(Vote) %>% 
  summarise(number_no_votes = n(),
            share_no_votes = n()*100/nrow(all_votes))

apply(all_votes,2,function(x) sum(is.na(x)))

all_votes.data <- all_votes %>% select(Vote_num,VoteRule,cabinet_name,ches_cmp_comb,
                                       gov_eu_supporter,Interinstitutional_Code,
                                       image_lag6m,gov_eu_cmp_static,year,VoteRule,
                                       gov_lr_cmp_static , eu_cont_gdp , presidency_vote,
                                       inflation,Actor,Transcription,
                                       unemployment, 
                                       Council_Config_final) %>% 
  filter(complete.cases(.))

all_votes.data$eu_cont_gdp <- scale(all_votes.data$eu_cont_gdp)
all_votes.data$inflation <- scale(all_votes.data$inflation)
all_votes.data$gov_lr_cmp_static <- scale(all_votes.data$gov_lr_cmp_static)
all_votes.data$unemployment <- scale(all_votes.data$unemployment)
all_votes.data$image_lag6m <- scale(all_votes.data$image_lag6m)
all_votes.data$budget <- ifelse(grepl("budget",tolower(all_votes.data$Interinstitutional_Code)),"Budget Vote","Not a Budget Vote")

all_votes.data$budget <- all_votes.data$budget %>% 
  as.factor() %>% 
  relevel("Not a Budget Vote")

#### Main Voting Model ####
m1 <- glmer(Vote_num ~ gov_eu_supporter*image_lag6m+
                               gov_lr_cmp_static+eu_cont_gdp+budget+
                               unemployment+inflation+Council_Config_final+
                               (1 | Actor)+(1|Interinstitutional_Code), data = all_votes.data, 
                             family = binomial, control = glmerControl(optimizer = "bobyqa"),
                             nAGQ = 1)

all_votes_no_pres <- all_votes.data %>% filter(presidency_vote=="No")
m2 <- glmer(Vote_num ~ gov_eu_supporter*image_lag6m+
                           gov_lr_cmp_static+eu_cont_gdp+budget+
                           unemployment+inflation+Council_Config_final+
                           (1 | Actor)+(1|Interinstitutional_Code), data = all_votes_no_pres, 
                         family = binomial, control = glmerControl(optimizer = "bobyqa"),
                         nAGQ = 1)

m3 <- glmer(Vote_num ~ gov_eu_supporter*image_lag6m+
                              gov_lr_cmp_static+eu_cont_gdp+budget+
                              unemployment+inflation+Council_Config_final+year+
                              (1 | Actor)+(1|Interinstitutional_Code), data = all_votes.data, 
                            family = binomial, control = glmerControl(optimizer = "bobyqa"),
                            nAGQ = 1)

m4 <- glmer(Vote_num ~ ches_cmp_comb*image_lag6m+
                           gov_lr_cmp_static+eu_cont_gdp+budget+
                           unemployment+inflation+Council_Config_final+
                           (1 | Actor)+(1|Interinstitutional_Code), data = all_votes.data, 
                         family = binomial, control = glmerControl(optimizer = "bobyqa"),
                         nAGQ = 1)

stargazer(m1,m2,m3,m4,type="text")

# This is Table E2
stargazer(m1,m2,m3,m4,
                     dep.var.labels   = "Opposition Vote",
                     column.labels = c("Baeline Model","Without Presidency","Year FE",
                                       "CHES+CMP"),omit = "year",
                     covariate.labels = c("Eurosceptic government", "Eurosceptic government (CHES+CMP)", "Public image of the EU", 
                                          "Government left-right position", "Net receipts from EU budget",
                                          "Budget issue","Unemployment rate","Inflation rate",
                                          "Council configuration: Ecofin","Council configuration: EPSCO",
                                          "Council configuration: ENV","Council configuration: JHA",
                                          "Eurosceptic government x Public image of the EU",
                                          "Eurosceptic government (CHES+CMP) x Public image of the EU",
                                          "Intercept"),
                     add.lines = list(c("Year-Fixed Effects","No","No","Yes","No")))

m.plot.gov <- ggeffect(m1, type = "pred", 
                                  terms = c("image_lag6m[all]","gov_eu_supporter"))

data.ranges <- all_votes.data %>% 
  group_by(gov_eu_supporter) %>% 
  summarise(min_values = min(image_lag6m),
            max_values = max(image_lag6m))

m.plot.gov <- m.plot.gov %>% 
  filter(group=="Europhile Government" & between(x,
                                                 data.ranges %>% filter(gov_eu_supporter=="Europhile Government") %>% select(min_values) %>% unlist(),
                                                 data.ranges %>% filter(gov_eu_supporter=="Europhile Government") %>% select(max_values) %>% unlist())|
           group=="Eurosceptic Government" & between(x,
                                                     data.ranges %>% filter(gov_eu_supporter=="Eurosceptic Government") %>% select(min_values) %>% unlist(),
                                                     data.ranges %>% filter(gov_eu_supporter=="Eurosceptic Government") %>% select(max_values) %>% unlist()))

m.plot.gov$group <- m.plot.gov$group %>% 
  recode("Europhile Government" = "Pro-EU \nGovernment",
         "Eurosceptic Government" = "Eurosceptic \nGovernment")

p_voting <- ggplot(m.plot.gov,aes(x=x,y=predicted))+
  geom_smooth(se=F,size=0.75,colour="black")+
  geom_smooth(aes(y=conf.low),size=0.75,se=F,lty="dashed",colour="black")+
  geom_smooth(aes(y=conf.high),size=0.75,se=F,lty="dashed",colour="black")+
  facet_wrap(~group)+
  labs(y="Predicted Probability of \nOpposition Vote",x="Public Image of the EU (z-score)",
       title="b) Opposition Voting in the Council")+
  theme_bw()+
  theme(strip.background = element_rect(fill = "white", colour = NA),
        strip.text = element_text(colour = "black",face = "bold"),
        axis.title = element_text(size=8,colour="black"),
        axis.text = element_text(colour = "black"))

# This plots Figure 5
ggpubr::ggarrange(p_participation,p_voting,ncol=1, nrow=2)
ggsave("figures_main_paper/figure_5.eps", width = 6, height = 7, units = "in")

tiff("figures_main_paper/figure_5.tiff", units="in", width=6, height=7, res=1000)
ggpubr::ggarrange(p_participation,p_voting,ncol=1, nrow=2)
endoffile <- dev.off()   
  
########## Voting significance for Eurosceptics ####

tab_bin <- calc_interaction_regression(var_1 = "image_lag6m",
                                       var_moderator = "gov_eu_supporter",
                                       reg_model = m1)

# This is Table E1
xtable::xtable(bind_rows(tab_bin %>% select(-moderator_level) %>%  mutate(Government = c("Pro-EU","Eurosceptic"))) %>% 
                 dplyr::rename("t-value" = "tval",
                               "p-value" = "p")) %>% 
  print(include.rownames=FALSE)

########## Participation significance for Eurosceptics ####

tab_participation_interaction <- calc_interaction_regression(var_1 = "image_lag6m_scaled",
                                                             var_moderator = "gov_eu_supporter",
                                                             reg_model = mod_speech_participation_1)

# This is Table D2
xtable::xtable(bind_rows(tab_participation_interaction %>% select(-moderator_level) %>%  mutate(Government = c("Pro-EU","Eurosceptic"))) %>% 
                 dplyr::rename("t-value" = "tval",
                               "p-value" = "p"))%>% 
  print(include.rownames=FALSE)

#### Changing threshold for voting model ####

storage_dat <- data.frame(to_try=c(-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9),
                          estimate = NA,
                          upper = NA,
                          lower = NA,
                          zvalue = NA,
                          pvalue = NA)

summary(all_votes.data$gov_eu_cmp_static)
for(i in 1:nrow(storage_dat)){
  if(is.na(storage_dat$estimate[i])){
    all_votes.data$gov_eu_supporter_robust <- ifelse(all_votes.data$gov_eu_cmp_static>storage_dat$to_try[i],
                                                     "Europhile Government",
                                                     "Eurosceptic Government")
    
    mod_voting <- glmer(Vote_num ~ gov_eu_supporter_robust*image_lag6m+
                          gov_lr_cmp_static+eu_cont_gdp+budget+
                          unemployment+inflation+Council_Config_final+
                          (1 | Actor)+(1|Interinstitutional_Code), data = all_votes.data, 
                        family = binomial, control = glmerControl(optimizer = "bobyqa"),
                        nAGQ = 1)
    
    sum.mod <- summary(mod_voting)
    sum.mod <- sum.mod$coefficients %>% 
      data.frame()
    sum.mod$term <- row.names(sum.mod)
    sum.mod <- sum.mod %>% 
      filter(term=="gov_eu_supporter_robustEurosceptic Government:image_lag6m")
    
    storage_dat$estimate[i] <- sum.mod$Estimate
    storage_dat$upper[i] <- sum.mod$Estimate+1.96*sum.mod$Std..Error
    storage_dat$lower[i] <- sum.mod$Estimate-1.96*sum.mod$Std..Error
    storage_dat$zvalue[i] <- sum.mod$z.value
    storage_dat$pvalue[i] <- sum.mod$Pr...z..
    print(i)
  }
}

quantile(all_votes.data$gov_eu_cmp_static,na.rm=T,)
ecdf(all_votes.data$gov_eu_cmp_static)(0)
ecdf(all_votes.data$gov_eu_cmp_static)(0.8)

storage_dat %>% 
  filter(upper<0) %>% 
  select(to_try,upper)

# This is Figure E1
ggplot(storage_dat %>% filter(to_try<1.5),aes(x=to_try,y=estimate,ymin=lower,ymax=upper))+
  geom_point()+
  geom_errorbar()+
  geom_hline(yintercept=0,lty="dashed")+
  labs(x = "Cutoffs that Define Governments as Eurosceptic",
       y = "Estimate and 95% Confidence Interval \nof Interaction Term")+
  theme_minimal()
ggsave("figures_appendix/figure_e_1.eps", width = 4.5, height = 4, units = "in")

